package org.ytor.core.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * created by yangtong on 2025/6/17 13:06:34
 * <br/>
 * 监控定时任务执行的情况
 */
@Slf4j
@Aspect
@Component
public class TaskExecutionMonitor {

    @Around("execution(* org.ytor.common.scheduler.Task.doTask(..))")
    public Object monitorTaskExecution(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        try {
            return joinPoint.proceed();
        } catch (Throwable e) {
            log.error("任务执行失败：{}", e.getMessage());
            throw e;
        } finally {
            if (log.isDebugEnabled()) {
                log.debug("执行耗时: {}", System.currentTimeMillis() - startTime);
            }
        }

    }

}
